#
import json
import time

import requests as requests
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

from config import webdriverPath, hostUrl


class execobj:
    i = 0
    item = None

    def set(self, item):
        self.i = item['currQuantity']
        self.list = []
        self.item = item
        self.citationList = []

    def run(self):
        # url_root = 'https://webofscience-clarivate-cn-s.era.lib.swjtu.edu.cn/wos/alldb/basic-search'
        # 创建ChromeOptions对象
        chrome_options = webdriver.ChromeOptions()
        # 禁止加载图片等资源
        chrome_options.add_argument("--disable-images")
        chrome_options.add_argument("--disable-plugins")
        chrome_options.add_argument("--disable-extensions")
        # 设置全屏参数
        chrome_options.add_argument("--start-maximized")

        # 创建WebDriver对象时传入ChromeOptions
        self.driver = webdriver.Chrome(options=chrome_options, service=Service(webdriverPath))
        self.driver.get('https://mzj.sh.gov.cn/websearch/#search/query=%E7%A4%BE%E4%BC%9A%E5%B7%A5%E4%BD%9C')  # 打开的页面




    def getList(self):
        # 点击
        # 等待元素加载
        try:
            # 或者等待直到某个元素可见
            WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR,'#maya-search-view > div > div > ul > li:nth-child(3)'))
            )
        except Exception as e:
            print("元素加载失败:", e)
            self.driver.quit()
            return
        self.driver.find_element(By.CSS_SELECTOR,'#maya-search-view > div > div > ul > li:nth-child(3)').click()
        time.sleep(2)

        # 获取元素
        self.getListItem()

    def getList2(self):
        # 社会组织
        self.driver.get('https://mzj.sh.gov.cn/websearch/#search/query=%E7%A4%BE%E4%BC%9A%E7%BB%84%E7%BB%87')
        try:
            # 或者等待直到某个元素可见
            WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR,'#maya-search-view > div > div > ul > li:nth-child(3)'))
            )
            # WebDriverWait(self.driver, 10).until(
            #     EC.visibility_of_element_located((By.CSS_SELECTOR,'#maya-search-filter > span:nth-child(2) > ul > li:nth-child(2) > a'))
            # )
        except Exception as e:
            print("元素加载失败:", e)
            self.driver.quit()
            return
        time.sleep(5)
        btnE= self.driver.find_element(By.CSS_SELECTOR,'#maya-search-filter > span:nth-child(2) > button')
        self.driver.execute_script("arguments[0].click();", btnE)
        time.sleep(1)

        btnE= self.driver.find_element(By.CSS_SELECTOR,'#maya-search-filter > span:nth-child(2) > button')
        self.driver.execute_script("arguments[0].click();", btnE)
        time.sleep(1)

        self.driver.find_element(By.CSS_SELECTOR,'#maya-search-filter > span.dropdown.open > ul > li:nth-child(2) > a').click()

        time.sleep(2)
        # 点击
        # 等待元素加载
        self.driver.find_element(By.CSS_SELECTOR,'#maya-search-view > div > div > ul > li:nth-child(3)').click()
        time.sleep(2)

        # 获取元素
        self.getListItem()

    def getListItem(self):
        try:
            source = '上海市民政局'
            while True:

                items = self.driver.find_elements(By.CSS_SELECTOR,
                                                  '#maya-search-result  div.maya-result-item')
                # 遍历元素
                for i in items:
                    # 获取元素的属性值
                    a1 = i.find_element(By.CSS_SELECTOR, 'a.doc-title')
                    title = a1.text
                    if title.find('社会组织') == -1:
                        continue
                    href = a1.get_attribute('href')
                    date = i.find_element(By.CSS_SELECTOR, 'div:nth-child(2) > span:nth-child(1)').text
                    abstractInfo = i.find_element(By.CSS_SELECTOR, 'div.doc-content').text

                    print(source, title, href, date,abstractInfo)
                    requests.post(hostUrl + '/policy/save',
                                  json={'documentName': title, 'accessionNo': href,
                                        'url': href, 'date': date + ' 00:00:00',
                                        'type': 'mzj.sh.gov',
                                        'source': source,
                                        'abstractInfo': abstractInfo,
                                        'status': '10'})
                    # return
                # 点击下一页
                nextBtn = self.driver.find_element(By.CSS_SELECTOR,'#maya-search-result ul.pagination > li:last-child > span')
                if nextBtn.text != '»':
                    break
                nextBtn.click()
                time.sleep(3)
            # 返回
        except Exception as e:
            print(e)


    def detail(self):
        while True:
            res = requests.post(hostUrl + '/policy/list', json={'type': 'mzj.sh.gov', })
            if res.json().get('code') != 200:
                break
            data = res.json().get('data')
            try:
                self.driver.get(data['url'])
                time.sleep(2)
                # 等待元素加载
                # 或者等待直到某个元素可见
                WebDriverWait(self.driver, 10).until(
                    EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                      '#ivs_title'))
                )
                print("元素加载成功")

                # 获取元素
                fullText = self.driver.find_element(By.CSS_SELECTOR,
                                                    '#main > div > div').get_attribute(
                    'innerHTML')
                accessionNo = ''
                try:
                    accessionNo = self.driver.find_element(By.CSS_SELECTOR,
                                                           '#ivs_content > table.table-bordered.table-hover > tbody > tr:nth-child(1) > td:nth-child(2)').text
                    a = self.driver.find_element(By.CSS_SELECTOR,
                                             '#ivs_content > table.table-bordered.table-hover > tbody > tr:nth-child(1) > td:nth-child(4)').text
                    a1 = self.driver.find_element(By.CSS_SELECTOR,
                                             '#ivs_content > table.table-bordered.table-hover > tbody > tr:nth-child(2) > td:nth-child(2)').text
                    a2 = self.driver.find_element(By.CSS_SELECTOR,
                                             '#ivs_content > table.table-bordered.table-hover > tbody > tr:nth-child(2) > td:nth-child(4)').text
                    data['infoData'] = json.dumps({'索取号': accessionNo
                                        ,'发布机构': a
                                        ,'文件编号': a1
                                        ,'公开类别': a2
                                        },ensure_ascii=False)
                except Exception as e:
                    print(e)

                data['accessionNo'] = accessionNo or data['accessionNo']
                data['fullText'] = fullText
                data['status'] = '20'
                print(data)
                requests.post(hostUrl + '/policy/save',
                              json=data)

            except Exception as e:
                data['status'] = '30'
                requests.post(hostUrl + '/policy/save',
                              json=data)
                print(e)
    def detail2(self):
        while True:
            res = requests.post(hostUrl + '/policy/list', json={'type': 'mzj.sh.gov', })
            if res.json().get('code') != 200:
                break
            data = res.json().get('data')
            try:
                self.driver.get(data['url'])
                time.sleep(2)
                # 等待元素加载
                # 或者等待直到某个元素可见
                WebDriverWait(self.driver, 10).until(
                    EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                      '#ivs_title'))
                )
                print("元素加载成功")

                # 获取元素
                fullText = self.driver.find_element(By.CSS_SELECTOR,
                                                    '#main > div > div').get_attribute(
                    'innerHTML')
                pdfurl = self.driver.find_element(By.CSS_SELECTOR,'#main .download0301')
                abstractInfo = data['abstractInfo']

                if pdfurl:
                    fullText = pdfurl.get_attribute('href')
                    abstractInfo ='\r\n '.join(e.text for e in self.driver.find_elements(By.CSS_SELECTOR,'#ivs_content > p'))


                accessionNo = ''
                try:
                    accessionNo = self.driver.find_element(By.CSS_SELECTOR,
                                                           '#ivs_content > table.table-bordered.table-hover > tbody > tr:nth-child(1) > td:nth-child(2)').text
                    a = self.driver.find_element(By.CSS_SELECTOR,
                                             '#ivs_content > table.table-bordered.table-hover > tbody > tr:nth-child(1) > td:nth-child(4)').text
                    a1 = self.driver.find_element(By.CSS_SELECTOR,
                                             '#ivs_content > table.table-bordered.table-hover > tbody > tr:nth-child(2) > td:nth-child(2)').text
                    a2 = self.driver.find_element(By.CSS_SELECTOR,
                                             '#ivs_content > table.table-bordered.table-hover > tbody > tr:nth-child(2) > td:nth-child(4)').text
                    data['infoData'] = json.dumps({'索取号': accessionNo
                                        ,'发布机构': a
                                        ,'文件编号': a1
                                        ,'公开类别': a2
                                        },ensure_ascii=False)
                except Exception as e:
                    print(e)

                print(data)
                data['accessionNo'] = accessionNo or data['accessionNo']
                data['fullText'] = fullText
                data['abstractInfo'] = abstractInfo
                data['status'] = '20'
                # print(data)
                requests.post(hostUrl + '/policy/save',
                              json=data)

            except Exception as e:
                data['status'] = '30'
                requests.post(hostUrl + '/policy/save',
                              json=data)
                print(e)

# https://mzj.sh.gov.cn
if __name__ == '__main__':
    a = execobj()
    a.run()
    # a.getList2()
    # a.getList()
    a.detail2()
    # a.detail()
    a.driver.quit()
    time.sleep(5)
